package com.drinks.yiniang.adapter.listview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.common.UploadImageJson;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.listener.CheckDoubleClickListener;
import com.hansen.library.listener.OnListItemClickListener;
import com.hansen.library.utils.CommonUtils;

import java.util.List;

/**
 * Created by han on 2018/11/1 0001.
 */

public class UploadImageGridAdapter extends BaseAdapter {

    private final int TYPE_ADD = 0x0;
    private final int TYPE_ITEM = 0x01;
    private final int ITEM_COUNT = 2;

    private Context mContext;

    private List<UploadImageJson.UploadImageData> mList;

    private LayoutInflater inflater;

    private OnListItemClickListener onListItemClickListener;

    //最大图片数量(为-1时不做限制)
    private int mMaxNum = 0;

    public UploadImageGridAdapter(Context context, List<UploadImageJson.UploadImageData> mList) {
        this.mContext = context;
        this.mList = mList;

        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;

        switch (getItemViewType(position)) {
            case TYPE_ITEM:
                if (convertView == null) {
                    mHolder = new ViewHolder();

                    convertView = inflater.inflate(R.layout.item_grid_upload_image, null);

                    mHolder.iv_grid_upload_pic = convertView.findViewById(R.id.iv_grid_upload_pic);
                    mHolder.iv_grid_upload_delete = convertView.findViewById(R.id.iv_grid_upload_delete);

                    convertView.setTag(mHolder);
                } else {
                    mHolder = (ViewHolder) convertView.getTag();
                }

                GlideUtils.loadAvatar(mContext, mHolder.iv_grid_upload_pic, mList.get(position).getOriginal());

                if (onListItemClickListener != null) {
                    mHolder.iv_grid_upload_delete.setOnClickListener(new CheckDoubleClickListener() {
                        @Override
                        public void onCheckDoubleClick(View v) {
                            onListItemClickListener.onItemClick(v, position);
                        }
                    });
                }
                break;
            case TYPE_ADD:
                convertView = inflater.inflate(R.layout.item_add_img, null);
                break;
            default:
                break;
        }

        return convertView;
    }

    @Override
    public int getCount() {
        return mMaxNum > 0 ? (CommonUtils.getListSize(mList) == mMaxNum ? CommonUtils.getListSize(mList) : CommonUtils.getListSize(mList) + 1) : (CommonUtils.getListSize(mList) + 1);
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        return position == CommonUtils.getListSize(mList) ? TYPE_ADD : TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return ITEM_COUNT;
    }

    public void setOnListItemClickListener(OnListItemClickListener onListItemClickListener) {
        this.onListItemClickListener = onListItemClickListener;
    }

    public void setmMaxNum(int mMaxNum) {
        this.mMaxNum = mMaxNum;
    }

    class ViewHolder {
        RatioRoundImageView iv_grid_upload_pic;
        ImageView iv_grid_upload_delete;
    }
}
